<?php

namespace app\store\controller\market;

use app\store\controller\Controller;
use app\store\model\wxapp\Formid as FormidModel;
use app\store\service\wxapp\Message as MessageService;
use \think\Db;
use think\Cache;
use app\common\model\Wxapp as WxappModel;
use app\store\model\Coupon as CouponModel;
use app\store\model\UserCoupon as UserCouponModel;


/**
 * 消息推送 (废弃)
 * Class Push
 * @package app\store\controller\market
 */
class Push extends Controller
{
    /**
     * 发送消息
     * @return array|mixed
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
         // 分割符号
    const SEPARATOR = ',';
    public function send()
    {
        if (!$this->request->isAjax()) {
                $list = Db::name('coupon')->select();
              return $this->fetch('send', compact('list'));
        }

         if ($this->request->isAjax()) {
             
        $data = $this->request->param();  
        $user_id = $data['send']['user_id'];
        $coupon_id = $data['send']['coupon_id'];
       $wxConfig = WxappModel::getWxappCache();
    //一键群发所有会员
    if($user_id=='-1'){
      $user= Db::name('user')->where(['is_delete'=>0,'wxapp_id'=> $wxConfig['wxapp_id']])->field('user_id')->select();  
      
    foreach ($user as $userId) {
                $this->sendyhq($userId['user_id'], $coupon_id,$wxConfig);
        } 
    }
             
       // 判断用户id集
      
     else{
        $userIdsArr = !strstr($user_id, self::SEPARATOR) ? [$user_id]
            : explode(self::SEPARATOR, $user_id);
        // 批量发送
        foreach ($userIdsArr as $userId) {
                $this->sendyhq($userId, $coupon_id,$wxConfig);
        }      
         }
    }
               return $this->renderSuccess('', '', [
            'stateSet' =>  $wxConfig
        ]);
  }
   private function sendyhq($user_id, $coupon_id,$wxConfig){
    
        $coupon = Db::name('coupon')->where('coupon_id',$coupon_id)->find();
        // 计算有效期
        if ($coupon['expire_type'] == 10) {
            $start_time = time();
            $end_time = $start_time + ($coupon['expire_day'] * 86400);
        } else {
            $start_time = $coupon['start_time'];
            $end_time = $coupon['end_time'];
        }
        // 整理领取记录
        $data = [
            'coupon_id' => $coupon_id,
            'name' => $coupon['name'],
            'color' => $coupon['color'],
            'coupon_type' => $coupon['coupon_type'],
            'reduce_price' => $coupon['reduce_price'],
            'discount' => $coupon['discount'],
            'min_price' => $coupon['min_price'],
            'expire_type' => $coupon['expire_type'],
            'expire_day' => $coupon['expire_day'],
            'start_time' => $start_time,
            'end_time' => $end_time,
            'apply_range' => $coupon['apply_range'],
            'user_id' => $user_id,
            'is_expire'=>0,
            'wxapp_id' => $wxConfig['wxapp_id'],
            'update_time'=>time(),
            'create_time'=>time()
        ];

      //     // 添加领取记录
         Db::name('user_coupon')->insert($data);
         return $this->fetch('send');
}
    /**
     * 活跃用户列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function user()
    {
        $list = (new FormidModel)->getUserList();
        return $this->fetch('user', compact('list'));
    }

}